package com.zg.generator.util;

import com.zg.generator.config.EngineConfig;

/**
 * @author zxb
 */
public class NameUtil{

	private static final String UNDER_LINE = "_";

	public static String genHumpStr(String str) {
		str = str.toLowerCase();

		while(str.startsWith(UNDER_LINE)) {
			str = str.substring(1);
		}

		while(str.endsWith(UNDER_LINE)) {
			str = str.substring(0,str.length()-1);
		}

		if(!str.contains(UNDER_LINE)) {
			return str;
		}

		String[] arr = str.split(UNDER_LINE);
		StringBuilder sb = new StringBuilder();
		sb.append(arr[0]);
		for(int i=1;i<arr.length;i++) {
			String s = arr[i];
			if(s!=null&&!"".equals(s)) {
				String first = s.substring(0, 1);
				String other = s.substring(1);
				sb.append(first.toUpperCase()).append(other);
			}
		}
		return sb.toString();
	}

	public static String genProName(String columnName) {
		return genHumpStr(columnName);
	}

	public static String genStaticProName(String columnName) {
		return columnName.toUpperCase();
	}

	public static String genClassName(String tableName) {
		String humpStr = genHumpStr(tableName);
		return humpStr.substring(0, 1).toUpperCase() +
				humpStr.substring(1);
	}

	public static String genMapping(String tableName) {
		tableName = tableName.toLowerCase();

		while(tableName.startsWith(UNDER_LINE)) {
			tableName = tableName.substring(1);
		}

		while(tableName.endsWith(UNDER_LINE)) {
			tableName = tableName.substring(0,tableName.length()-1);
		}

		if(!tableName.contains(UNDER_LINE)) {
			return "/"+tableName;
		}

		String[] arr = tableName.split(UNDER_LINE);
		StringBuilder sb = new StringBuilder();
		sb.append("/").append(arr[0]);
		for(int i=1;i<arr.length;i++) {
			String s = arr[i];
			if(s!=null&&!"".equals(s)) {
				sb.append("/").append(s);
			}
		}
		return sb.toString();
	}

	public static String genEntityImport(String tableName) {
		String humpStr = genHumpStr(tableName);
		String sb = humpStr.substring(0, 1).toUpperCase() +
				humpStr.substring(1);
		return EngineConfig.packageName.get() + sb;
	}

	public static void main(String[] args) {
		String str = "aaa_bbb_ccc";
		String result = NameUtil.genHumpStr(str);
		System.out.println(str+"==========>"+result);

		String columnName = "el_cust_id";
		columnName = columnName.substring(0,columnName.indexOf("_id"));
		System.out.println(NameUtil.genHumpStr(columnName));

		String tableName = "el_cust";
		System.out.println(NameUtil.genEntityImport(tableName));
	}

}
